<?php


namespace app\api\controller;

use app\api\service\DadaService;
use think\Config;
use think\Controller;
use app\demo\controller\DadaOpen;


class Dada extends Controller
{

    //商户注册达达
    public function register($mobile, $city_name, $enterprise_name, $enterprise_address, $contact_name, $email)
    {
        $config['source_id'] = '';
        $config['url'] = 'http://newopen.imdada.cn/merchantApi/merchant/add';

        $dada = new DadaService($config);

        $data = array(
            'mobile' => $mobile,
            'city_name' => $city_name,
            'enterprise_name' => $enterprise_name,   //企业全称
            'enterprise_address' => $enterprise_address,   //企业地址
            'contact_name' => $contact_name,
            'contact_phone' => $mobile,
            'email' => $email,
        );

        $reqStatus = $dada->makeRequest($data);
        return $reqStatus;
    }

    //新增门店
    public function add($source_id, $origin_shop_id, $station_name, $business, $city_name, $area_name, $station_address, $contact_name, $phone, $lng, $lat)
    {
        $config['source_id'] = $source_id;
        $config['url'] = 'http://newopen.imdada.cn/api/shop/add';

        $dada = new DadaService($config);

        $data = array(
            'station_name' => $station_name,     //门店名称
            'business' => $business,           //业务类型
            'city_name' => $city_name,          //城市名称(如,上海)
            'area_name' => $area_name,          //区域名称(如,浦东新区)
            'station_address' => $station_address,    //门店地址
            'lng' => $lng,                 //门店经度
            'lat' => $lat,                 //门店纬度
            'contact_name' => $contact_name,      //联系人姓名
            'phone' => $phone,      //联系人电话
            'origin_shop_id' => $origin_shop_id,      //门店编码
        );
        $body[0] = $data;
        $reqStatus = $dada->makeRequest($body);
        return $reqStatus;
    }

    //门店更新
    public function update($source_id, $origin_shop_id, $station_name, $business, $city_name, $area_name, $station_address, $lng, $lat, $contact_name, $phone)
    {
        $config['source_id'] = $source_id;
        $config['url'] = 'http://newopen.imdada.cn/api/shop/update';

        $dada = new DadaService($config);

        $data = array(
            'origin_shop_id' => $origin_shop_id,     //门店编码
            'station_name' => $station_name,     //门店名称
            'business' => $business,           //业务类型
            'city_name' => $city_name,          //城市名称(如,上海)
            'area_name' => $area_name,          //区域名称(如,浦东新区)
            'station_address' => $station_address,    //门店地址
            'lng' => $lng,                 //门店经度
            'lat' => $lat,                 //门店纬度
            'contact_name' => $contact_name,      //联系人姓名
            'phone' => $phone,      //联系人电话
        );

        $reqStatus = $dada->makeRequest($data);
        return $reqStatus;
    }

    // 1. 查询订单运费
    public function queryDeliverFee($source_id, $shop_no, $origin_id, $city_code, $tips, $info, $cargo_price, $is_prepay, $expected_fetch_time, $receiver_name, $receiver_address, $receiver_phone, $callback)
    {
        $config['source_id'] = $source_id;  //'73753';
        $config['url'] = 'http://newopen.imdada.cn/api/order/queryDeliverFee';

        $dada = new DadaService($config);

        $data = array(
            'shop_no' => $shop_no,  //'11047059',   //门店编号
            'origin_id' => $origin_id,   //第三方订单ID
            'city_code' => $city_code,    //订单所在城市的code
            'tips' => $tips,             //小费（单位：元，精确小数点后一位）
            'info' => $info,     //订单备注
            'cargo_price' => $cargo_price,     //订单金额
            'is_prepay' => $is_prepay,        //是否需要垫付 1:是 0:否 (垫付订单金额，非运费)
            'expected_fetch_time' => time() + (int)$expected_fetch_time * 60,
            'receiver_name' => $receiver_name,
            'receiver_address' => $receiver_address,
            'receiver_phone' => $receiver_phone,
            'callback' => $callback
        );

        $reqStatus = $dada->makeRequest($data);
        return $reqStatus;
    }

    // 2. 查询运费后发单
    public function addAfterQuery($source_id, $deliveryNo)
    {
        $config['source_id'] = $source_id; //'73753';
        $config['url'] = 'http://newopen.imdada.cn/api/order/addAfterQuery';

        $dada = new DadaService($config);
        $data = array(
            'deliveryNo' => $deliveryNo,
        );

        $reqStatus = $dada->makeRequest($data);
        return $reqStatus;
    }

    //订单详情查询
    public function queryOrder($source_id, $order_id)
    {
        $config['source_id'] = $source_id;  //'73753';
        $config['url'] = 'http://newopen.imdada.cn/api/order/status/query';

        $dada = new DadaService($config);

        $data = array(
            'order_id' => $order_id,   //第三方订单编号
        );

        $reqStatus = $dada->makeRequest($data);
        return $reqStatus;
    }


    //订单取消
    public function formalCancel($source_id, $order_id, $cancel_reason_id)
    {
        $config['source_id'] = $source_id; //'73753';
        $config['url'] = 'http://newopen.imdada.cn/api/order/formalCancel';

        $dada = new DadaService($config);

        $data = array(
            'order_id' => $order_id,   //第三方订单编号
            'cancel_reason_id' => $cancel_reason_id,  //取消原因ID
        );

        $reqStatus = $dada->makeRequest($data);
        return $reqStatus;
    }

    //订单取消的原因
    public function reasons()
    {
        $config['source_id'] = '73753';    //$source_id;
        $config['url'] = 'http://newopen.qa.imdada.cn/api/order/cancel/reasons';

        $dada = new DadaService($config);

        $data = array(
            'body' => '',
        );

        $reqStatus = $dada->makeRequest($data);
        return $reqStatus;
    }


    //获取充值链接
    public function recharge($source_id, $amount, $category, $notify_url)
    {
        $config['source_id'] = $source_id;
        $config['url'] = 'http://newopen.imdada.cn/api/recharge';

        $dada = new DadaService($config);

        $data = array(
            'amount' => $amount,   //充值金额（单位元，可以精确到分）
            'category' => $category,
            'notify_url' => $notify_url,
        );

        $reqStatus = $dada->makeRequest($data);
        return $reqStatus;
    }

    //查询余额
    public function select_money($source_id)
    {
        $config['source_id'] = $source_id;
        $config['url'] = 'http://newopen.imdada.cn/api/balance/query';

        $dada = new DadaService($config);

        $data = array(
            'category'=>3
        );

        $reqStatus = $dada->makeRequest($data);
        return $reqStatus;
    }


    //查询地址Code
    public function cityCode()
    {
        $config['url'] = 'http://newopen.qa.imdada.cn/api/cityCode/list';
        $config['source_id'] = '73753';
        $dada = new DadaService($config);
        $data = [
            'name' => '',
        ];
        $reqStatus = $dada->makeRequest($data);
        return $reqStatus;
    }

}
